﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Commonlib;
namespace AlbonWeb.Manager.MessageManager
{
    public partial class MessageList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["limit"] = "where 1=1";
                bindRep();
                
            }
        }
        protected void bindRep()
        {
            ViewState["limit"] = "where 1=1";
            if (Request["orderid"] != null)
            {
                ViewState["limit"] += " and orderID=" + Request["orderid"].ToString();
            }
            else
            {
                ViewState["limit"] += " and (sender='" + User.Identity.Name + "' or receiver='" + User.Identity.Name + "')";
            }
            if (tbSender.Text.Trim() != "")
            {
                ViewState["limit"] += " and sender like '%"+tbSender.Text.Trim()+"%'";
            }
            if (tbReceiver.Text.Trim() != "")
            {
                ViewState["limit"] += " and sender like '%" + tbReceiver.Text.Trim() +"%'";
            }
            string cmdstr = "select count(*) from MessageView " + ViewState["limit"].ToString();
            AspNetPager1.RecordCount = int.Parse(SqlHelper.ExecuteScalar(SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null).ToString());
            SqlParameter[] parameters = new SqlParameter[]{
                new SqlParameter("@tablename","MessageView"),
                new SqlParameter("@startIndex",AspNetPager1.StartRecordIndex),
                new  SqlParameter("@endIndex",AspNetPager1.EndRecordIndex),
                new SqlParameter("@limit",ViewState["limit"].ToString()),
                new SqlParameter("@orderby","createDate desc")
            };
            SqlHelper.BindRepeater(repMain, SqlHelper.SqlConnectionString, CommandType.StoredProcedure, "netpager", parameters);
            
        }

        protected void newMessage_Click(object sender, EventArgs e)
        {
            if (Request["orderid"] != null)
            {
                Response.Redirect("/Manager/MessageManager/MessageNew.aspx?orderid=" + Request["orderid"].ToString());
            }
            else
            {
                Response.Redirect("/Manager/MessageManager/MessageNew.aspx");
            }   
        }

        //换页
        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            bindRep();
        }

    }
}